package AdamsDevice;

import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Vector;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;





/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class RemoteApplet extends javax.swing.JApplet {
	public static final String SERVICE_TYPE = "_film_streaming._udp.local.";
	public static final String SERVICE_TYPE2 = "_screen_service._tcp.local.";
	private JButton starterButton;
	private JScrollPane jScrollPane1;
	private JButton DeviceToggle;
	private JButton playButton;
	private JButton pauseButton;
	private JButton stopButton;
	private JButton returnButton;
	private JButton tempButton;
	private JButton listGetter;
	private JButton deviceOptionButton;
	private JTextArea consoleWindow;
	private JButton refreshButton;
	private JmDNS jmdns = null;
	public String selectedDevice = "command";
	public ServiceEvent Device1 =null;
	public TCPSocketClientremote chatter;
	public Vector<JButton> listy = null;
	public Vector<JButton> DeviceButtonList = null;
	public Vector<ServiceEvent> DeviceList = new Vector<ServiceEvent>();


	/**
	 * Auto-generated main method to display this 
	 * JApplet inside a new JFrame.
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				RemoteApplet inst = new RemoteApplet();
				frame.getContentPane().add(inst);
				((JComponent)frame.getContentPane()).setPreferredSize(inst.getSize());
				frame.pack();
				frame.setVisible(true);
			}
		});

	}

	public RemoteApplet() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setSize(500, 380);
			getContentPane().setLayout(null);
			{
				starterButton = new JButton();
				getContentPane().add(starterButton);
				starterButton.setText("start");
				starterButton.setBounds(12, 12, 64, 23);
				starterButton.addActionListener(new java.awt.event.ActionListener() {		
					public void actionPerformed(java.awt.event.ActionEvent e) {
						DeviceButtonList = new Vector<JButton>();
						starterButton.setVisible(false);
						refreshButton.setVisible(true);
						consoleWindow.setVisible(true);
						jScrollPane1.setVisible(true);
						try {
							consoleWindow.setText("");
							jmdns = JmDNS.create();
							jmdns.addServiceListener(SERVICE_TYPE, new SampleListener());
							consoleWindow.append("looking for service of type: "+ SERVICE_TYPE + "\n");
							jmdns.addServiceListener(SERVICE_TYPE2, new SampleListener());
							consoleWindow.append("looking for service of type: "+ SERVICE_TYPE2 + "\n");
						} catch (IOException e1) {
							e1.printStackTrace();
						}

					}
				});
			}
			{
				refreshButton = new JButton();
				getContentPane().add(refreshButton);
				refreshButton.setText("finish");
				refreshButton.setVisible(false);
				refreshButton.setBounds(12, 12, 75, 23);

				refreshButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						
						refreshButton.setVisible(false);
						deviceOptionButton.setVisible(false);
						listGetter.setVisible(false);
						clearDeviceList();
						DeviceButtonList = null;
						DeviceList = null;
						clearList();
						listy = null;
						starterButton.setVisible(true);	
						jmdns.unregisterAllServices();
						jmdns.close();
						consoleWindow.append("CLOSED COMMUNICATION"+ "\n");
						
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(145, 35, 279, 155);
				{
					consoleWindow = new JTextArea();
					jScrollPane1.setViewportView(consoleWindow);
					consoleWindow.setVisible(false);
					consoleWindow.setText(" ");
					consoleWindow.setBounds(155, 35, 293, 155);
				}
			}
			{
				deviceOptionButton = new JButton();
				getContentPane().add(deviceOptionButton);
				deviceOptionButton.setText(selectedDevice);
				deviceOptionButton.setVisible(false);
				deviceOptionButton.setBounds(155, 221, 100, 23);
				deviceOptionButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
						String msg = JOptionPane.showInputDialog("send console message");
						chatter.sendMessage(msg);

					}
				});
			}
			{
				listGetter = new JButton();
				getContentPane().add(listGetter);
				listGetter.setText("list");
				listGetter.setBounds(340, 221, 100, 23);
				listGetter.setVisible(false);
				listGetter.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
						chatter.sendMessage("list");
						Vector<String> temp = chatter.movieList;
						if(listy != null){
							clearList();
						}			
						listy = new Vector<JButton>(); //resets the list for updates
						for(int i = 0; i < temp.size(); i++){
							final String tempMovieName = temp.get(i);
							tempButton = new JButton();
							getContentPane().add(tempButton);
							tempButton.setText(tempMovieName);
							tempButton.setBounds(60, 250 + (30*i), tempMovieName.length()*10, 20);
							tempButton.addActionListener(new java.awt.event.ActionListener() {
								public void actionPerformed(java.awt.event.ActionEvent e) {
									chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
									chatter.sendMessage("play->"+tempMovieName);	
									runCheck();
								}
							});
							listy.add(tempButton);

						}
					}
				});
			}
			{
				returnButton = new JButton();
				getContentPane().add(returnButton);
				returnButton.setText("back");
				returnButton.setBounds(76, 306, 80, 60);
				returnButton.setVisible(false);
				returnButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
						chatter.sendMessage("command->back");	
						chatter.play_mode = false;
						runCheck();
					}
				});
			}
			{
				stopButton = new JButton();
				getContentPane().add(stopButton);
				stopButton.setText("stop");
				stopButton.setBounds(155, 306, 80, 60);
				stopButton.setVisible(false);
				stopButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
						chatter.sendMessage("command->stop");	
					}
				});
			}
			{
				pauseButton = new JButton();
				getContentPane().add(pauseButton);
				pauseButton.setText("pause");
				pauseButton.setBounds(234, 306, 80, 60);
				pauseButton.setVisible(false);
				pauseButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
						chatter.sendMessage("command->pause");	
					}
				});
			}
			{
				playButton = new JButton();
				getContentPane().add(playButton);
				playButton.setText("play");
				playButton.setBounds(313, 306, 80, 60);
				playButton.setVisible(false);
				playButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						chatter = new TCPSocketClientremote(Device1.getInfo().getHostAddress(),Device1.getInfo().getPort(), consoleWindow);
						chatter.sendMessage("command->play");	
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//test method to see if the play_mode is now true
	public void runCheck(){

		for(JButton temp: listy){
			temp.setVisible(!chatter.play_mode);
		}
		listGetter.setVisible(!chatter.play_mode);
		returnButton.setVisible(chatter.play_mode);
		stopButton.setVisible(chatter.play_mode);
		pauseButton.setVisible(chatter.play_mode);
		playButton.setVisible(chatter.play_mode);
	}


	public void clearList(){
		for(JButton temp: listy){
			temp.setVisible(false);
		}
	}

	public void clearDeviceList(){
		for(JButton temp: DeviceButtonList){
			temp.setVisible(false);
		}
	}



	//set of controls for the jmDNS
	class SampleListener implements ServiceListener {
		public void serviceAdded(final ServiceEvent event) {

			String ans = JOptionPane.showInputDialog("found device: ["+ event.getName()+ "] \n do you want to Connect? y/n");

			if (ans.equalsIgnoreCase("y")){
				event.getDNS().requestServiceInfo(event.getType(), event.getName(), 0);
			}else{
				consoleWindow.append("device ignored" + "\n");
			}
			//event.getDNS().requestServiceInfo(event.getType(), event.getName(), 0);
		}

		public void serviceRemoved(ServiceEvent event) {
			consoleWindow.append("Service removed : " + event.getName() + "." + event.getType());
			System.out.println("Service removed : " + event.getName() + "." + event.getType());
		}

		public void serviceResolved(ServiceEvent event) {
			// Display some information about the service.
			consoleWindow.append("Service resolved: " + event.getInfo().getName() + ", host: "
					+ event.getInfo().getHostAddress() + ", port: " + event.getInfo().getPort()+ "\n");

			System.out.println("Service resolved: " + event.getInfo().getName() + ", host: "
					+ event.getInfo().getHostAddress() + ", port: " + event.getInfo().getPort());

			DeviceList.addElement(event);
			final ServiceEvent temp = DeviceList.lastElement();
			DeviceToggle = new JButton();
			getContentPane().add(DeviceToggle);
			DeviceToggle.setText(event.getName());
			DeviceToggle.setBounds(436, 30+(DeviceList.size()*30), event.getName().length()*10, 23);	
			DeviceToggle.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Device1 = temp;
					consoleWindow.append("using device: "+ Device1.getName() +"\n");
					selectedDevice = Device1.getName();
					//deviceOptionButton.setVisible(true);
					listGetter.setVisible(true);
				}
			});
			
			DeviceButtonList.add(DeviceToggle);
		}
	}


}
